package com.mofe.zxy.util;

import com.mofe.zxy.exception.BusinessException;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Objects;

/**
 * @ClassName: SecurityUtil
 * @Description: SecurityUtil
 * @Create by: ZhangXuYuan
 * @Date: 2022/4/13 16:11
 */


public class SecurityUtil {

    private static final  String ADMIN_ID = "adminId";

//    public static Integer getAdminId() {
//        OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails)SecurityContextHolder.getContext().getAuthentication().getDetails();
//
//        Map<String,Object> decodedDetails = (Map<String,Object>)details.getDecodedDetails();
//        return (Integer) decodedDetails.get("adminId");
//    }

    public static Integer getAdminIdFromHeader() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();

        String headerAdminId = request.getHeader(ADMIN_ID);

        Integer adminId = null;

        try {
            adminId = Integer.valueOf(headerAdminId);
        }catch (Exception e) {
            throw  new BusinessException("invalid token");
        }

        return adminId;
    }
}
